<template>
  <div class="menu-wrapper">
    <div class="desktop-top">
      <div class="system-logo">
        <el-icon class="logo-icon"><Management /></el-icon>
        <span>管理系统</span>
      </div>
    </div>
    <div class="desktop-menu">
      <el-menu
          class="el-menu-vertical-demo"
          text-color="#fff"
          active-text-color="#ffd04b"
          background-color="#001529"
          :collapse-transition="false"
          unique-opened
          router
          :default-openeds="defaultOpeneds"
          :default-active="defaultActive"
          @select="handleMenuSelect"
      >
        <el-sub-menu index="/order">
          <template #title>
            <el-icon><Menu /></el-icon>
            <span>订单管理</span>
          </template>
          <el-menu-item index="/order/orderQuery">订单查询</el-menu-item>
        </el-sub-menu>

        <!-- 规则配置模块（如需启用可取消注释） -->
        <!-- <el-sub-menu index="/rule">
          <template #title>
            <el-icon><Setting /></el-icon>
            <span>规则配置</span>
          </template>
          <el-menu-item index="/rule/labelConfig">面单规则配置</el-menu-item>
        </el-sub-menu> -->

        <!-- 系统配置模块（如需启用可取消注释） -->
        <!-- <el-sub-menu index="/sys">
          <template #title>
            <el-icon><Location /></el-icon>
            <span>系统配置</span>
          </template>
          <el-menu-item index="/sys/cosConfig">Cos服务配置</el-menu-item>
        </el-sub-menu> -->

        <el-menu-item index="/labelPrint">
          <el-icon><Document /></el-icon>
          <span>单号扫描打印</span>
        </el-menu-item>
      </el-menu>
    </div>

    <!-- 侧边栏底部信息 -->
    <div class="sidebar-footer">
      <div class="version-info">
        <span>v1.0.0</span>
      </div>
    </div>
  </div>
</template>

<script>
import { Document, Menu, Management } from '@element-plus/icons-vue'
import { defineComponent, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'

export default defineComponent({
  name: 'LabelSidebar',
  components: {
    Document,
    Menu,
    Management
  },
  setup() {
    const router = useRouter()
    const route = useRoute()

    // 默认选中的菜单项
    const defaultActive = computed(() => {
      // 如果路由已有路径，则使用当前路径，否则使用默认路径
      return route.path === '/' || route.path === '' ? '/order/orderQuery' : route.path
    })

    // 计算默认展开的菜单
    const defaultOpeneds = computed(() => {
      // 总是默认展开订单管理菜单
      const defaultOpen = ['/order']

      // 检查当前路由是否属于其他子菜单
      const paths = route.path.split('/')
      if (paths.length > 2 && paths[1] !== 'order') {
        return [`/${paths[1]}`]
      }

      return defaultOpen
    })

    // 处理菜单选择事件
    const handleMenuSelect = (index) => {
      router.push(index)
    }

    // 组件挂载时，如果是首次加载，重定向到订单查询
    onMounted(() => {
      if (route.path === '/' || route.path === '') {
        router.replace('/order/orderQuery')
      }
    })

    return {
      defaultOpeneds,
      defaultActive,
      handleMenuSelect
    }
  }
})
</script>

<style lang="scss" scoped>
/* 样式部分保持不变 */
.menu-wrapper {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #001529;
  color: #fff;
  box-shadow: 2px 0 6px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;

  .desktop-top {
    padding: 0 16px;
    height: 64px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);

    .system-logo {
      display: flex;
      align-items: center;
      gap: 8px;

      .logo-icon {
        font-size: 20px;
        color: #409eff;
      }

      span {
        font-size: 16px;
        font-weight: 500;
        letter-spacing: 0.5px;
      }
    }
  }

  .desktop-menu {
    flex: 1;
    overflow-y: auto;

    // 自定义滚动条样式
    &::-webkit-scrollbar {
      width: 4px;
    }

    &::-webkit-scrollbar-track {
      background: transparent;
    }

    &::-webkit-scrollbar-thumb {
      background: rgba(255, 255, 255, 0.2);
      border-radius: 2px;
    }

    &::-webkit-scrollbar-thumb:hover {
      background: rgba(255, 255, 255, 0.3);
    }
  }

  .el-menu {
    border-right: none !important;

    // 菜单悬停效果
    .el-menu-item,
    .el-sub-menu {
      transition: all 0.2s ease;

      &:hover {
        background-color: rgba(255, 255, 255, 0.05) !important;
      }
    }

    // 当前选中菜单样式
    .el-menu-item.is-active {
      background-color: rgba(64, 158, 255, 0.2) !important;
      border-right: 3px solid #409eff;
    }
  }

  .sidebar-footer {
    padding: 12px 0;
    border-top: 1px solid rgba(255, 255, 255, 0.1);

    .version-info {
      text-align: center;
      font-size: 12px;
      color: rgba(255, 255, 255, 0.5);
    }
  }
}
</style>